﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MS.INV
{
    public partial class MReport : DevExpress.XtraEditors.XtraForm
    {
        public MReport()
        {
            InitializeComponent();

            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.TopLevel = false;
        }

        private DevExpress.XtraTab.XtraTabPage GetTabPage(string v_Name)
        {
            foreach (DevExpress.XtraTab.XtraTabPage tp in tabs.TabPages)
            {
                if (tp.Name.ToLower() == v_Name.ToLower())
                    return tp;
            }
            return null;
        }

        private bool CheckExists(string v_Name)
        {
            foreach (DevExpress.XtraTab.XtraTabPage tp in tabs.TabPages)
            {
                if (tp.Name.ToLower() == v_Name.ToLower())
                {
                    tabs.SelectedTabPage = tp;
                    return true;
                }
            }

            return false;
        }

        private void AddForm(Form ff)
        {
            if (!CheckExists(ff.Name))
            {
                ff.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                ff.TopLevel = false;

                DevExpress.XtraTab.XtraTabPage tp = tabs.TabPages.Add();
                tp.Name = ff.Name;
                tp.Text = ff.Text;
                tp.Controls.Add(ff);
                ff.Dock = DockStyle.Fill;
                ff.Show();

                ff.Disposed += new EventHandler(ff_Disposed);
                tabs.SelectedTabPage = tp;
            }
        }

        void ff_Disposed(object sender, EventArgs e)
        {
            Form f = (Form)sender;
            DevExpress.XtraTab.XtraTabPage tp = GetTabPage(f.Name);
            if (tp != null)
                tp.Dispose();
        }

        private void treeRpt_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.Name.ToLower() == "IC_Stock".ToLower())
            {
                AddForm(new Reports.InventoryStock());
            }
            else if (e.Node.Name.ToLower() == "ProfitByTime".ToLower())
            {
                AddForm(new Reports.Profits.ByTime());
            }
            else if (e.Node.Name.ToLower() == "Profit_Month".ToLower())
            {
                AddForm(new Reports.Profits.Profit_Chart_Month());
            }
            else if (e.Node.Name.ToLower() == "Profit_Year".ToLower())
            {
                AddForm(new Reports.Profits.Profit_Chart_Year());
            }
            else if (e.Node.Name.ToLower() == "ProfitCompare".ToLower())
            {
                AddForm(new Reports.Profits.Profit_Chart_Compare());
            }
            else if (e.Node.Name.ToLower() == "Q_ByTime".ToLower())
            {
                AddForm(new Reports.Qtys.Qty_ByTime());
            }
            else if (e.Node.Name.ToLower() == "Qty_ChartMonth".ToLower())
            {
                AddForm(new Reports.Qtys.Qty_Chart_Month());
            }
            else if (e.Node.Name.ToLower() == "Qty_ChartYear".ToLower())
            {
                AddForm(new Reports.Qtys.Qty_Chart_Year());
            }
            else if (e.Node.Name.ToLower() == "Costs_ByTime".ToLower())
            {
                AddForm(new Reports.Costs.Costs_ByTime());
            }
            else if (e.Node.Name.ToLower() == "Costs_ChartYear".ToLower())
            {
                AddForm(new Reports.Costs.Costs_Chart_Year());
            }
            else if (e.Node.Name.ToLower() == "Costs_ChartYear_Compare".ToLower())
            {
                AddForm(new Reports.Costs.Costs_Chart_Compare());
            }
        }

        private void Main_Load(object sender, EventArgs e)
        {
            treeRpt.Nodes[0].ExpandAll();
        }

        private void tabs_CloseButtonClick(object sender, EventArgs e)
        {
            DevExpress.XtraTab.XtraTabPage tp = tabs.SelectedTabPage;
            Form f = (Form)tp.Controls[0];
            f.Close();
        }
    }
}
